//
//  MyRealPlayViewController.m
//  ysdemo
//
//  Created by 6CIT－APPLE on 15/11/4.
//  Copyright © 2015年 6CIT－APPLE. All rights reserved.
//

#import "RealPlayViewController.h"
#import "YSPlayerController.h"
#import "YSHTTPClient.h"

#define CIF_VEDIO_RATIO  (16 / 9.0f)      // 标准CIF视频宽高比

@interface RealPlayViewController () <YSPlayerControllerDelegate> {
    UIView               *_playView;                // 播放界面
    UIButton             *_backButton;             // 返回按键
    YSPlayerController   *_realCtrl;              // 播放控制层
}

- (void)goBack:(id)sender;

@end

@implementation RealPlayViewController

- (void)setAccessToken:(NSString *)accessToken {
    [[YSHTTPClient sharedInstance]setClientAccessToken:accessToken];
    _accessToken = accessToken;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _realCtrl = [[YSPlayerController alloc]initWithDelegate:self];
    
    CGFloat screenHeigh = [UIScreen mainScreen].bounds.size.width; // 由于是横屏所以宽度与高度对调
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.height; // 由于是横屏所以宽度与高度对调
    CGFloat playWidth = screenWidth;
    CGFloat playHeight = playWidth / CIF_VEDIO_RATIO;
    CGFloat playY = (screenHeigh - playHeight) / 2;
    
    _playView = [[UIView alloc]initWithFrame:CGRectMake(0, playY, playWidth, playHeight)];
    _playView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_playView];

    _backButton = [[UIButton alloc]initWithFrame:CGRectMake(screenWidth - 50, 10, 40, 38)];
    [_backButton setTitle:@"退出" forState:UIControlStateNormal];
    [_backButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_backButton addTarget:self  action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_backButton];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self realPlay];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    [self stopRealPlay];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)goBack:(id)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)realPlay {
    [_realCtrl startRealPlayWithCamera:_cameraId accessToken:_accessToken inView:_playView];
}

- (void)stopRealPlay {
    [_realCtrl stopRealPlay];
}

#pragma player controller delegate
- (void)playerOperationMessage:(YSPlayerMessageType)msgType withValue:(id)value {
    dispatch_async(dispatch_get_main_queue(), ^{
        
        switch (msgType) {
            case YSPlayerMsgCheckFail: {
                UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"错误提示" message:@"用户授权失败，请重新登录" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
                break;
            case YSPlayerMsgRealPlayStart:
                break;
            case YSPlayerMsgRealPlaySuccess:
                break;
            case YSPlayerMsgRealPlayStop:
                break;
            default:
                break;
        }
        
    });
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}

@end
